#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time: 2022/9/30 19:34
# @Author: 徐洪英
# @Email: xx@gmail.com
# @File：tools.py

import hashlib
import yaml
from config.config import DATA_PATH, CASE_PATH,DOCUMNET_PATH
import sys
import os


def hash_code(password):
    md5 = hashlib.md5()#获取md5对象
    md5.update(password.encode('utf-8'))#将传入的pwd编码后，更新md5
    return md5.hexdigest()#返回十六进制的MD5码


def set_res_data(s):
    s = s.replace('":"', "=").replace('":', '=')
    return s


def read_yaml(file):
    with open(file, encoding='utf-8') as f:
        data = yaml.load(f, yaml.Loader)
    return data


# import base64
#
# decoded = base64.b64decode(open('1.txt', 'rb').read())
# open('2.jpg', 'wb').write(decoded)  # 保存




# import yaml
# f = open('datas/test_document.txt.yaml', encoding='utf—8')
# data = yaml.load(stream=f, Loader=yaml.Loader)
# print(type(data))
# print(data)


def gen_case():
    """根据数据文件和模板文件生成测试用例"""
    # 把datas里面的yaml文件找出来。
    list_data = os.listdir(DATA_PATH)
    print(list_data) #查出datas下的所有文件
    yaml_files = [] #建一个空列表，把有关yaml的文件放在列表中
    for file in list_data: #循环文件
    #     # 后缀名为yaml和yml的文件
        if file.endswith('.yaml') :  #求出已yaml文件结尾的
        # or file.endswith('.yml')  #以yml文件结尾的
    #         # print(file)
            yaml_files.append(file)
    for file in yaml_files: #判断是否在这个文件里面
    #     # 操作xxxxx.yaml文件
        data_name = file
        class_name = file.split('.')[0].title().replace("_", "")
        # print(class_name)
        case_name = file.split('.')[0]
    #     print(case_name)
    #     # 读出模板文件，替换内容，写入用例模块
        filename = os.path.join(DOCUMNET_PATH,'test_document.txt')
        with open(filename, encoding='utf-8') as f:
            content = f.read()
    #     # 替换
        content= content.format(class_name=class_name,
                                data_name=data_name,
                                case_name=case_name)
        #写入用例模块
        case_filename = os.path.join(CASE_PATH,"{}.py".format(case_name))
        with open(case_filename,'w',encoding='utf-8') as f:
            f.write(content)

if __name__ == '__main__':
    gen_case()